﻿using System;
using System.Collections.Generic;
using Fee.Stores;
using Identity.Stores;
using Identity.ViewModels;

namespace Fee.ViewModel
{
    public class FrameworkContractDetailViewModel
    {
        public FrameworkContractDetailViewModel()
        {
        }

        public FrameworkContractDetailViewModel(FrameworkContract contract)
        {
            if (contract == null) throw new ArgumentNullException(nameof(contract));
            Id = contract.Id;
            Name = contract.Name;
            No = contract.No;
            ContractType = contract.ContractType;
            Vendor = contract.Vendor != null
                ? new VendorSimpleViewModel
                {
                    Id = contract.Vendor.Id,
                    Name = contract.Vendor.Name
                }
                : null;
            Comment = contract.Comment;
            Amount = contract.Amount;
            ReductionAmount = contract.ReductionAmount;
            Status = contract.Status;

            SignTime = contract.SignTime;
            ExpireTime = contract.ExpireTime;
            ContractDescription = contract.ContractDescription;
            AmountWithoutTax = contract.AmountWithoutTax;
            NetType = contract.NetType;
            EffectYear = contract.EffectYear;

        }

        public string NetType { get; set; }
        public string EffectYear { get; set; }

        /// <summary>
        ///     获取合同名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        ///     合同号
        /// </summary>
        public string No { get; set; }

        /// <summary>
        ///     合同类型
        /// </summary>
        public ContractType ContractType { get; set; }

        /// <summary>
        ///     乙方机构
        /// </summary>
        public VendorSimpleViewModel Vendor { get; set; }


        /// <summary>
        ///     合同费用
        /// </summary>
        public decimal Amount { get; set; }

        public decimal AmountWithoutTax { get; set; }
        public int Id { get; set; }


        /// <summary>
        ///     降点信息
        /// </summary>
        public decimal ReductionAmount { get; set; }

        public ContractStatus Status { get; set; }


        /// <summary>
        ///     签订时间
        /// </summary>
        public DateTimeOffset? SignTime { get; set; }

        /// <summary>
        ///     框加结束日期
        /// </summary>
        public DateTimeOffset? ExpireTime { get; set; }

        public string ContractDescription { get; set; }

        public string Comment { get; set; }

        public FrameworkContract ToFrameworkContract<T>(IFrameworkContractStore<T> framworkStore,
            IVendorStore<T> vendorStore, IProjectStore<T> projectStore)
            where T : FeeDbContext
        {
            var contract = Id == 0
                ? new FrameworkContract
                {
                    Id = Id
                }
                : framworkStore.GetById(Id, true);

            contract.Comment = Comment;
            contract.Name = Name;
            contract.No = No;
            contract.ContractType = ContractType;
            contract.Vendor = Vendor != null ? vendorStore.GetById(Vendor.Id) : null;
            contract.Amount = Amount;
            contract.Status = Status;
            contract.ReductionAmount = ReductionAmount;
            contract.AmountWithoutTax = AmountWithoutTax;
            contract.ContractDescription = ContractDescription;
            contract.SignTime = SignTime;
            contract.ExpireTime = ExpireTime;





            return contract;
        }
    }
}